import lexical_analyzer

list = lexical_analyzer.result_list
i = 0 






#<算术表达式> → 项A

def calculate(list):
    global i
    
    if item(list):        
        if A(list):
            return True
    return False

#A→（+  <项>|-  <项> ）
def A(list):
    global i    
    if list[i] == 43:
        i+=1
        
        if item(list):
            return True
    elif list[i] == 45:
        i+=1    
        if item(list):
            return True
    return False

#<项> → <标识符>
#<项> → <整数>
def item(list):
    global i
    if list[i] == 36:
        i+=1
        
        return True
    elif list[i] == 37:        
        i+=1
        return True
    return False

#<布尔表达式>→ <项>  <  <项>
def bools(list):
    global i
    if item(list):
        if list[i] == 53:
            i+=1
            
            if item(list):
                return True
    return False

# <复合句> → begin <语句表> end
def is_statement(list):
    global i
    
    if list[i] == 3:        
        i+=1
        
        if is_lexical_list(list):
            
            if list[i] == 12:
                i+=1
                
                return True       
    return False
#语句表
    
#语句表 → 语句 S

    
def is_lexical_list(list):
    if is_lexical(list):
        if S(list):
            return True
    return False

   #S → 语句; S | ε
def S(list):
    global i
    if list[i] == 52:           
        i+=1
        
        if is_lexical(list):
            if S(list):
                return True
    elif list[i] != 52:
        
        return True
    return False 



#语句
def is_lexical(list):
    global i
    if whiles(list):
        return True
    elif repeats(list):
        return True
    elif fuzhis(list):
        return True
    
    return False
#<语句> → <while句>
def whiles(list):
    global i
    
    if list[i] == 34:
        i+=1
       
        if bools(list):
            if list[i] == 10:
                i+=1
                
                if is_lexical(list):
                    return True
    return False
#<语句> → <repeat句>
def repeats(list):
    global i
    if list[i] == 26:
        i+=1
        
        if is_lexical(list):
            if list[i] == 32:
                i+=1 
                if bools(list):
                    return True
    return False
#<语句> → <赋值句>
def fuzhis(list):
    global i
    if list[i] == 36:
        i+=1
       
        if list[i] == 51:
            i+=1            
            if calculate(list):
                return True       
    return False

#<程序> → program <标识符> ；<变量说明> <复合句> .
#<变量说明> → var <标识符> ：<类型> ；
def is_legal_syntax(list):
    global i
    if list[i] == 23:
        i+=1
        if list[i] == 36:
            i+=1
            if list[i] == 52:
                i+=1
                if list[i] == 33:
                    i+=1
                    if list[i] == 36:
                        i+=1
                        if list[i] == 50:
                            i+=1
                            
                            if list[i] == 4 or list[i] == 17:
                                i+=1
                                
                                if list[i] == 52:
                                    i+=1                                    
                                    if is_statement(list):                                        
                                        if list[i] == 46:
                                            
                                            return True            
    return False

if __name__ == '__main__':
    src=  lexical_analyzer.read_file('TEST14.txt')
    result_list = lexical_analyzer.src_code_to_result_list(src)

    if(is_legal_syntax(result_list)):
        print("测试成功")
    else:
        print("测试失败")
    print(result_list)